package com.lanou.elh.sysmanage.system_member.web.controller;

import com.lanou.elh.sysmanage.system_member.entity.Tb_member;
import com.lanou.elh.sysmanage.system_member.service.impl.MemberShowAllImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberShowAllImpl showAll;

//    显示所有
    @GetMapping("/list")
    public List<Tb_member> showAll(Tb_member tbMember){
//        Map<String,Object> map = new HashMap<>();
//        map.put("total",showAll.countMember());
//        map.put("row",showAll.showMember(tbMember));
        List<Tb_member> list = showAll.showMember(tbMember);
        return list;
    }
//  显示未被删除数量
    @GetMapping("/count")
    public Integer memberCount(){
        return showAll.countMember();
    }
//    显示被删除数量
    @GetMapping("/count/remove")
    public Integer memberRemoveCount(){
        return showAll.removeCountMember();
    }
//  停用会员
    @PutMapping("/stop")
    public String memberStop(Tb_member tbMember){

        showAll.stopById(tbMember);
        return null;
    }
//  启用会员
    @PutMapping("/start")
    public String memberStart(Tb_member tbMember){
        showAll.startById(tbMember);
        return null;
    }

    //批量假删除会员
    @PutMapping("/remove")
    public String memberRemove(Long[] ids){
        for (Long id : ids) {
            showAll.remove(id);
        }
        return null;
    }
//    假删除会员
    @PutMapping("/onlyremove")
    public String memberDel(Long id){
        showAll.remove(id);
        return null;
    }
//    (循环)还原
    @PutMapping("/restart")
    public String memberRestart(Long[] ids){
        for (Long id : ids) {
            showAll.restart(id);
        }
        return null;
    }
//    (循环)真删除
    @DeleteMapping("/del")
    public String memberDelete(Long[] ids){
        for (Long id : ids) {
            showAll.delete(id);
        }
        return null;
    }
//    验证用户名
    @GetMapping("/username")
    public Boolean validateUsername(String username){
        if (showAll.memberByUsername(username)!=null){
            return false;
        }
        return true;
    }
//    验证电话
    @GetMapping("/phone")
    public Boolean validatePhone(String phone){
        if (showAll.memberByPhone(phone)!=null){
            return false;
        }
        return true;
    }
//    验证邮箱
    @GetMapping("/email")
    public Boolean validateEmail(String email){
        if (showAll.memberByEmail(email)!=null){
            return false;
        }
        return true;
    }
//     添加新会员
    @PostMapping("/add")
    public String memeberRegister(Tb_member tbMember){
        showAll.memberRegister(tbMember);
        return "member-list";
    }
//    修改会员
    @PostMapping("/update")
    public String memberUpdate(Tb_member tbMember){
        showAll.memberUpdate(tbMember);
        return "member-list";
    }
//    修改密码
    @PostMapping("/change")
    public String memberChangPass(Tb_member tbMember){
        showAll.memberChangPass(tbMember);
        return "member-list";
    }
}
